﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace SG
{
    public class XmlLoader
    {
        private Dictionary<string, string> m_mapData = new Dictionary<string, string>();

        public bool LoadText(string strText)
        {
            try
            {
                m_mapData.Clear();
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(strText); //加载xml文件
                var data = (XmlNode)xmlDoc.SelectSingleNode("/root/Config");
                var nodeList = data.ChildNodes;
                foreach (var item in nodeList)
                {
                    if (item is XmlElement)
                    {
                        var i = item as XmlElement;
                        string key = i.LocalName;// i.GetAttribute("key");
                        string value = i.InnerText;
                        m_mapData.Add(key, value);
                    }
                }
                OnInit();
                return true;
            }
            catch (Exception ex)
            {

            }
            return false;
        }


        public void Load(string strFileName)
        {
            m_mapData.Clear();
            //Debug.Log (strFileName);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strFileName); //加载xml文件
            var data = (XmlNode)xmlDoc.SelectSingleNode("/root/Config");
            var nodeList = data.ChildNodes;
            foreach (var item in nodeList)
            {
                if (item is XmlElement)
                {
                    var i = item as XmlElement;
                    string key = i.LocalName;// i.GetAttribute("key");
                    string value = i.InnerText;
                    m_mapData.Add(key, value);
                    //	Debug.Log (i.Name + "  " + i.InnerText.ToString());
                }
            }
            OnInit();
        }

        public void SetValue(string strKey, string strValue)
        {
            m_mapData.Add(strKey, strValue);
        }

        public void Fresh(string fullName)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<root></root>");
            XmlElement element = doc.CreateElement("Config");
            foreach (var item in m_mapData)
            {
                XmlElement e = doc.CreateElement("Item");
                e.SetAttribute(item.Key, item.Value);
                element.AppendChild(e);
            }
            doc.DocumentElement.AppendChild(element);
            doc.Save(fullName);

        }
        protected virtual void OnInit() { }
        public int GetValueInt(string strKey)
        {
            if (m_mapData.ContainsKey(strKey))
                return Convert.ToInt32(m_mapData[strKey]);
            //Debug.Log("find inexistent key = " + strKey);
            return -1;
        }
        public double GetValueFloat(string strKey)
        {
            if (m_mapData.ContainsKey(strKey))
                return Convert.ToDouble(m_mapData[strKey]);
            //Debug.Log("find inexistent key = " + strKey);
            return 0;
        }
        public string GetValueString(string strKey)
        {
            if (m_mapData.ContainsKey(strKey))
                return Convert.ToString(m_mapData[strKey]);
            //Debug.Log("find inexistent key = " + strKey);
            return "";
        }

        public static float GetFloat(XmlElement e, string strKey)
        {
            var item = (XmlElement)e.SelectSingleNode(strKey);
            if (item != null)
                return (float)Convert.ToDouble(item.InnerText);
            return 0;
        }
        public static int GetInt(XmlElement e, string strKey)
        {
            var item = (XmlElement)e.SelectSingleNode(strKey);
            if (item != null)
                return Convert.ToInt32(item.InnerText);
            return 0;
        }
    }
}
